package com.chijun.mform.field.impl;


import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;

import com.chijun.mform.R;
import com.chijun.mform.create.mode.FormField;
import com.chijun.mform.field.BaseField;
import com.example.ccj.u.ActivityUtils;
import com.example.ccj.u.LogUtils;
import com.example.ccj.u.StringUtils;
import com.example.ccj.u.TimeUtils;
import com.github.gzuliyujiang.calendarpicker.CalendarPicker;

import io.reactivex.ObservableEmitter;

/**
 * 日期区间
 * @author 驰珺
 * whechat   : chijun9
 * create at : 2024/12/23 15:44
 */
public class FieldFor07RiQiQuJian extends BaseField {
    public FieldFor07RiQiQuJian(){
        super();
    }

    @Override
    public View getWritelView(Context context, WriteFormClass writeFormClass) {
        FormField formField = writeFormClass.formField;
        //提示文本
        if (formField.getHint() != null) writeFormClass.fieldFullContentView.setHint(formField.getHint());
        //输入文本颜色
        if (formField.getTextColor() != 0) writeFormClass.fieldFullContentView.setTextColor(formField.getTextColor());
        //是否只读
        if (formField.isReadOnly()) {
            writeFormClass.fieldFullContentView.setKeyListener(null);
            writeFormClass.fieldFullContentView.setFocusable(false);
        }

        String textFieldZanCunData = getTextFieldZanCunData(context,"fieldId=?",String.valueOf(formField.getId()));
        if (textFieldZanCunData == null) textFieldZanCunData = formField.getDefaultContent();
        //默认文本
        if (textFieldZanCunData != null) {
            try {
                String[] split = textFieldZanCunData.split("-");
                if (split.length == 2) {
                    split[0] = split[0].trim();
                    split[1] = split[1].trim();
                    long l = Long.parseLong(split[0]);
                    long l1 = Long.parseLong(split[1]);
                    formField.setDefaultContent(TimeUtils.millis2String(l) + " - " + TimeUtils.millis2String(l1));
                }
                saveValue = textFieldZanCunData;
            } catch (Exception e1) {
                LogUtils.e("日期区间转换失败。" + textFieldZanCunData);
            }
        }

        //文本改变事件
        writeFormClass.fieldFullContentView.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}
            @Override
            public void afterTextChanged(Editable s) {
                writeFormClass.inputIsOK = check(s,formField,writeFormClass.fieldFullWriteErrorView);
            }
        });

        writeFormClass.fieldFullContentView.setKeyListener(null);
        writeFormClass.fieldFullContentView.setFocusable(false);
        writeFormClass.fieldFullJumpImgView.setVisibility(View.VISIBLE);
        writeFormClass.fieldFullJumpImgView.setImageResource(R.drawable.field_icon_riqixuanze);
        writeFormClass.fieldFullJumpImgView.setOnClickListener(v -> {
            CalendarPicker calendarPicker = new CalendarPicker(ActivityUtils.getTopActivity());
            calendarPicker.setRangeDateOnFuture(3);
            long singleTimeInMillis = System.currentTimeMillis();
            calendarPicker.setSelectedDate(singleTimeInMillis,singleTimeInMillis);//设置默认选择的日期时间戳
            calendarPicker.setOnRangeDatePickListener((startDate, endDate) -> {
                String s = TimeUtils.date2String(startDate) + "  -  "+ TimeUtils.date2String(endDate);
                writeFormClass.fieldFullContentView.setText(s);
                saveValue = TimeUtils.date2Millis(startDate) + "-" + TimeUtils.date2Millis(endDate);
            });
            calendarPicker.show();
        });

        return writeFormClass.view;
    }



    @SuppressLint("SetTextI18n")
    @Override
    protected View getShowFullView(Context context, FullClass fullModel) {
        //字段名属性设置
        fullModel.setFieldProperty();
        //设置字段值
        fullModel.loadContent.setKeyListener(null);
        fullModel.loadContent.setFocusable(false);
        fullModel.loadContent.setTextIsSelectable(true);//可选择

        String fieldValue = fullModel.serverFullData.formOrdinaryFieldDataJsonObject.getString(String.valueOf(fullModel.formField.getId()));
        if (!StringUtils.isEmpty(fieldValue)) {
            try {
                LogUtils.d("返回的日期区间：" +fieldValue);
                String[] split = fieldValue.split("-");
                if (split.length == 2) {
                    String trim0 = split[0].trim();
                    String trim1 = split[1].trim();
                    long l0 = Long.parseLong(trim0);
                    long l1 = Long.parseLong(trim1);
                    String s0 = TimeUtils.millis2String(l0);
                    String s1 = TimeUtils.millis2String(l1);
                    fullModel.loadContent.setText(s0 + " - " + s1);
                } else {
                    LogUtils.e("数据保存错误,原始数据:" + fieldValue);
                    fullModel.loadContent.setText("数据错误：" + fieldValue);
                    fullModel.loadContent.setTextColor(Color.RED);
                }
            } catch (Exception e) {
                LogUtils.e(" "+e.getMessage());
//                fullModel.loadContent.setText("数据加载错误" + e.getMessage());
//                fullModel.loadContent.setTextColor(Color.RED);

            }
        }

        if (fullModel.isHaveModifyPower()) {
            fullModel.setJumpImageProperty(R.mipmap.form_field_date, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    CalendarPicker calendarPicker = new CalendarPicker(ActivityUtils.getTopActivity());
                    calendarPicker.setRangeDateOnFuture(3);
                    long singleTimeInMillis = System.currentTimeMillis();
                    calendarPicker.setSelectedDate(singleTimeInMillis,singleTimeInMillis);//设置默认选择的日期时间戳
                    calendarPicker.setOnRangeDatePickListener((startDate, endDate) -> {
                        String s = TimeUtils.date2String(startDate) + "  -  "+ TimeUtils.date2String(endDate);
                        fullModel.loadContent.setText(s);
                        processModifyFormFieldDataMap.put(fullModel.formField.getId(),TimeUtils.date2Millis(startDate) + "-" + TimeUtils.date2Millis(endDate));
                    });
                    calendarPicker.show();
                }
            });
        }

        return fullModel.layout;
    }

    /**
     * 字段设置界面
     * @param context 上下文
     * @param installClass 配置
     * @return 1
     */
    @Override
    protected View getFieldInstallView(Context context, InstallClass installClass) {
        ObservableEmitter<ViewGroup> emitter = installClass.emitter;

        emitter.onNext(installClass.getTimeTypeConfigView());     //设置日期类型
        emitter.onNext(installClass.getIsMastView());        //是否必填
        emitter.onNext(installClass.getIsPrintView());        //是否参与打印

        return installClass.v;
    }
}
